package yl.hs.bmipfull.controllers.vehiclemanage;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.vehiclemanage.RosteringChangeEntity;
import yl.hs.bmipfull.service.vehiclemanage.IRosteringChangeService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.ResultCode;
import yl.hs.bmipfull.viewmodels.*;

/**
 * @author 韩纪旺
 * @apiNote 车辆排班变更记录表控制层
 * @since 2022年09月14日 17时09分51秒
 */
@Api(value = "车辆排班变更记录表Web接口", tags = "管理--车辆排班变更记录表操作")
@RequestMapping(value = "/api/rosteringChange")
@RestController
public class RosteringChangeController extends ControllerBase {

    @Autowired
    IRosteringChangeService service;

    @ApiOperation(value = "查询车辆排班变更记录表", notes = "查询车辆排班变更记录表")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询车辆排班变更记录表", notes = "分页查询车辆排班变更记录表")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询车辆排班变更记录表", notes = "根据车辆排班变更记录表编号查询车辆排班变更记录表")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存车辆排班变更记录表", notes = "查询/更新车辆排班变更记录表信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saverostering_change(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saverostering_change(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除车辆排班变更记录表信息", notes = "根据车辆排班变更记录表编号删除系车辆排班变更记录表信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "车辆排班变更记录表编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String sn) {
        if (service.changeStateBySN(sn, DataState.DELETED.code) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }

    @ApiOperation(value = "更改车辆排班变更记录表状态", notes = "更改车辆排班变更记录表状态")
    @RequestMapping(path = "/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    public WebResult changeStateBySn(@ApiParam(value = "车辆排班变更记录表编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn, @ApiParam(value = "数据状态", name = "state", allowMultiple = true, required = true) @PathVariable("state") short state) {
        if (service.changeStateBySN(sn, state) > 0) {
            return success("更改成功");
        } else {
            return fail("更改失败");
        }

    }


    @ApiOperation(value = "车辆看板信息", notes = "车辆看板信息")
    @PostMapping("/bulletinBoardInformation/{page}/{rowNum}")
    public WebResult bulletinBoardInformation(@RequestBody PageQuery query,
                                              @ApiParam(value = "页码") @PathVariable int page,
                                              @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        PageResult<RosteringChangeEntity> result = null;
        try {
            result = service.bulletinBoardInformation(query, page, rowNum);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }

    }

    @ApiOperation(value = "车辆看板信息", notes = "判断是否可以更换司机")
    @PostMapping("/Judge")
    public WebResult Judge(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        try {
            Integer result = service.getJudpe(submitModel);
            if (result < 0) {
                return new WebResult(ResultCode.FAIL.code);
            }
            return success("更换成功");
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "车辆看板信息", notes = "更换司机")
    @PostMapping("/changeDriver")
    public WebResult changeDriver(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result=  service.changeDriver(submitModel.getData(),submitModel.getSn());
            if(result>0){
                return success("保存成功");
            }
            return fail("保存失败") ;
        } catch (Exception e) {
            return error(e);
        }
    }
}